/**
 * Created by leiweibo on 7/25/15.
 */

'use strict'

var app = angular.module('feecontroller', []);

app.controller('FeeCtrl',
  function( $scope, $window, $filter, PatientService, ProvinceService, HospitalService, OfficeService, DoctorService, FeeService, FeeData) {

    var pageState;

    $scope.$watch('time', function (newValue) {
      $scope.time = $filter('date')(newValue, 'yyyy/MM/dd');
    });

    $scope.provinces = ProvinceService.list(function(data) {
      $scope.provinces = data.provinces
    });

    $scope.provinceChanged = function(provinceId) {
      $scope.hospitals = HospitalService.list_by_province(provinceId, function(res) {
        if (res.result == false) {
          $scope.error_msg = res.message;
        }
        $scope.hospitals = res.hospitals;
      });
    }

    $scope.hospitalChanged = function (hospitalId) {
      OfficeService.list_by_hospital(hospitalId, function (res) {
        if (res.result == false) {
          $scope.error_msg = res.message;
        }

        $scope.offices = res.offices;
      });
    };

    $scope.officeChanged = function(officeId) {
      DoctorService.list_by_office(officeId, function(res) {
        if (res.result == false) {
          $scope.error_msg = res.message;
        }

        $scope.doctors = res.doctors;
      });
    };

    $scope.doctorChanged = function() {
      // province, hospital, office, doctor, disease, success) {
      PatientService.list($scope.office, $scope.doctor, function(res) {
        if (res.result == false) {
          $scope.error_msg = res.message;
        }

        $scope.patients = res.patients;
      });
    };

    $scope.createOrUpdate = function() {
      var formData = {
        id: $scope.id,
        province: $scope.province,
        hospital: $scope.hospital,
        office: $scope.office,
        doctor: $scope.doctor,
        patient: $scope.patient,
        time: $scope.time,
        fee: $scope.fee,
        desc: $scope.desc
      };

      if ($scope.id) {
        FeeService.update(formData, function (res) {
          if (res.result == true) {
              delete $scope.province,
              delete $scope.hospital,
              delete $scope.office,
              delete $scope.doctor,
              delete $scope.patient,
              delete $scope.time,
              delete $scope.fee,
              delete $scope.desc

            window.location = "#/patient_fee_list";
          } else {
            $scope.error_msg = res.message;
          }
        }, function (data, status) {
          $scope.error_msg = '无权限进行此操作';
        });
      } else {
        FeeService.create(formData, function (res) {
          if (res.result == true) {

            delete $scope.province,
              delete $scope.hospital,
              delete $scope.office,
              delete $scope.doctor,
              delete $scope.patient,
              delete $scope.time,
              delete $scope.fee,
              delete $scope.desc

            alert('添加成功');
          } else {
            $scope.error_msg = res.message;
          }
        }, function (data, status) {
          $scope.error_msg = '无权限进行此操作';
        });
      }
    }

    if (FeeData.get() && !$.isEmptyObject(FeeData.get())) {
      var data = FeeData.get();
      $scope.id = data._id;

      console.log(data);

      $scope.province_name = data.province_name;
      $scope.hospital_name = data.hospital_name;
      $scope.office_name = data.office_name;
      $scope.doctor_name = data.doctor_name;
      $scope.patient_name = data.patient.name;
      $scope.fee = data.fee;
      $scope.time = data.time;
      $scope.desc = data.desc;
    }

    $scope.callServer = function (tableState) {
      if (!tableState) {
        tableState = pageState
        tableState.pagination.start = 0;
      } else {
        pageState = tableState;
      }

      $scope.isLoading = true;

      var pagination = tableState.pagination;
      var pageSize = pagination.number || 20;
      var pageNo = Math.ceil((pagination.start + 1) / pageSize) || 1;
      FeeService.list(pageNo, pageSize, tableState, function (res) {
        $scope.displayed = res.patient_fees;
        tableState.pagination.numberOfPages = res.pageCount;
        $scope.isLoading = false;
      });
    };

    $scope.edit = function(pos) {
      var data = $scope.displayed[pos];
      if (data.patient) {
        FeeData.set(data);
        window.location = "#/update_patient_fee";
      } else {
        alert('信息数据不完整,请联系管理员');
      }
    }

    $scope.add = function() {
      FeeData.set(null);
      window.location = "#/add_fee";
    }

    $scope.delete = function (pos) {
      bootbox.setDefaults("locale", "zh_CN");
      bootbox.confirm("确定删除该费用信息吗", function (result) {
        if (result == true) {
          var fee = $scope.displayed[pos];
          if (fee) {
            var data = {id: fee._id};
            FeeService.delete(data, function (res) {
              if (res.result == true) {
                $scope.displayed.splice(pos, 1);
              } else {
                alert('删除数据失败');
              }
            }, function (data, status) {
            });
          } else {
            alert("请刷新重试");
          }
        }
      });
    };
  });
